<?php
namespace app\index\traits;

trait Pagepwd
{
	public function default()
	{
		$from_url = input('get.from_url', '');
		$re = request();
	    if ($re->isAjax()) {
	    	$pagepwd = input('post.pagepwd', '', 'trim');
	    	$is_error = $pagepwd == $this->secret ? 0 : 1;

	    	if ($is_error) {
	    		return json(['code' => 0,'msg' => '密码错误']);
	    	}

	    	$timestamp = time();
			$uuid = uuid();
			$payload = [
				'iss' => 'jwt_ttkan_api',  //该JWT的签发者
				'iat' => $timestamp,  //签发时间
				'exp' => $timestamp + 3600,  //过期时间 每次鉴权的时候去延长时间
				'uuid' => $uuid,
			];
			$token = \app\common\service\Jwt::getToken($payload, $this->secret);

    		return json([
    			'code' => 1,
    			'msg' => 'Success',
    			'data' => ['url'=>'/'.$from_url, 'token'=>$token],
    		]);
	    }

	    $module     = strtolower($re->module());
	    $controller = strtolower($re->controller());
	    $url = url($module.'/'.$controller.'/default').'?from_url='.$from_url;
	    $this->assign([
			'action_url' => $url,
		]);

		return $this->fetch('common/default');
	}
}